# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
        h = ListNode(0)
        h.next = head
        p = q = h
        for _ in range(k):
            if q:
                q = q.next
        while q:
            p, q = p.next, q.next
        return p